home *** CD-ROM | disk | FTP | other *** search
/ PC Basics 53 / PC Basics Issue 53.iso / Software / Internet / Invboard.exe / PC Basics 53 / Invboard / upload / sources / functions.php < prev    next >
Encoding:
PHP Script  |  2002-07-23  |  55.4 KB  |  1,769 lines

  1. <?php
  2.  
  3.  
  4. /*
  5. +--------------------------------------------------------------------------
  6. |   IBFORUMS v1
  7. |   ========================================
  8. |   by Matthew Mecham and David Baxter
  9. |   (c) 2001,2002 IBForums
  10. |   http://www.ibforums.com
  11. |   ========================================
  12. |   Web: http://www.ibforums.com
  13. |   Email: phpboards@ibforums.com
  14. |   Licence Info: phpib-licence@ibforums.com
  15. +---------------------------------------------------------------------------
  16. |
  17. |   > Multi function library
  18. |   > Module written by Matt Mecham
  19. |   > Date started: 14th February 2002
  20. |
  21. |    > Module Version Number: 1.0.0
  22. +--------------------------------------------------------------------------
  23. */
  24.  
  25.  
  26.  
  27. class FUNC {
  28.  
  29.     var $time_formats = array();
  30.     var $time_options = array();
  31.     var $offset       = "";
  32.     var $offset_set   = 0;
  33.  
  34.     // Set up some standards to save CPU later
  35.     
  36.     function FUNC() {
  37.         global $INFO;
  38.         
  39.         $this->time_options = array( 'JOINED' => $INFO['clock_joined'],
  40.                                      'SHORT'  => $INFO['clock_short'],
  41.                                      'LONG'   => $INFO['clock_long']
  42.                                    );
  43.         
  44.     }
  45.     
  46.     /*-------------------------------------------------------------------------*/
  47.     //
  48.     // Redirect using HTTP commands, not a page meta tag.
  49.     //
  50.     /*-------------------------------------------------------------------------*/
  51.     
  52.     function boink_it($url)
  53.     {
  54.         global $ibforums;
  55.         
  56.         if ($ibforums->vars['header_redirect'] == 'refresh')
  57.         {
  58.             
  59.             @header("Refresh: 0;url=".$url);
  60.         }
  61.         else
  62.         {
  63.             @header("Location: ".$url);
  64.         }
  65.         exit();
  66.     }
  67.     
  68.     /*-------------------------------------------------------------------------*/
  69.     //
  70.     // Create a random 8 character password
  71.     //
  72.     /*-------------------------------------------------------------------------*/
  73.     
  74.     function make_password()
  75.     {
  76.         $pass = "";
  77.         $chars = array(
  78.             "1","2","3","4","5","6","7","8","9","0",
  79.             "a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J",
  80.             "k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T",
  81.             "u","U","v","V","w","W","x","X","y","Y","z","Z");
  82.     
  83.         $count = count($chars) - 1;
  84.     
  85.         srand((double)microtime()*1000000);
  86.  
  87.         for($i = 0; $i < 8; $i++)
  88.         {
  89.             $pass .= $chars[rand(0, $count)];
  90.         }
  91.     
  92.         return($pass);
  93.     }
  94.     
  95.     /*-------------------------------------------------------------------------*/
  96.     //
  97.     // Generate the appropriate folder icon for a topic
  98.     //
  99.     /*-------------------------------------------------------------------------*/
  100.     
  101.     function folder_icon($topic, $dot="", $last_time=-1) {
  102.         global $ibforums;
  103.         
  104.         $last_time = $last_time > $ibforums->input['last_visit'] ? $last_time : $ibforums->input['last_visit'];
  105.         
  106.         if ($dot != "")
  107.         {
  108.             $dot = "_DOT";
  109.         }
  110.         
  111.         if ($topic['state'] == 'closed')
  112.         {
  113.             return $ibforums->skin['B_LOCKED'];
  114.         }
  115.         
  116.         if ($topic['poll_state'])
  117.         {
  118.         
  119.             if ( ! $ibforums->member['id'] )
  120.             {
  121.                 return $ibforums->skin['B_POLL'.$dot];
  122.             }
  123.             
  124.             if ($topic['last_post'] > $topic['last_vote'])
  125.             {
  126.                 $topic['last_vote'] = $topic['last_post'];
  127.             }
  128.             
  129.             if ($last_time  && ($topic['last_vote'] > $last_time ))
  130.             {
  131.                 return $ibforums->skin['B_POLL'.$dot];
  132.             }
  133.             if ($last_time  && ($topic['last_vote'] < $last_time ))
  134.             {
  135.                 return $ibforums->skin['B_POLL_NN'.$dot];
  136.             }
  137.             
  138.             return $ibforums->skin['B_POLL'];
  139.         }
  140.         
  141.         
  142.         if ($topic['state'] == 'moved' or $topic['state'] == 'link')
  143.         {
  144.             return $ibforums->skin['B_MOVED'];
  145.         }
  146.         
  147.         if ( ! $ibforums->member['id'] )
  148.         {
  149.             return $ibforums->skin['B_NORM'.$dot];
  150.         }
  151.         
  152.         if (($topic['posts'] + 1 >= $ibforums->vars['hot_topic']) and ( (isset($last_time) )  && ($topic['last_post'] <= $last_time )))
  153.         {
  154.             return $ibforums->skin['B_HOT_NN'.$dot];
  155.         }
  156.         if ($topic['posts'] + 1 >= $ibforums->vars['hot_topic'])
  157.         {
  158.             return $ibforums->skin['B_HOT'.$dot];
  159.         }
  160.         if ($last_time  && ($topic['last_post'] > $last_time))
  161.         {
  162.             return $ibforums->skin['B_NEW'.$dot];
  163.         }
  164.         
  165.         return $ibforums->skin['B_NORM'.$dot];
  166.         
  167.     }
  168.     
  169.     /*-------------------------------------------------------------------------*/
  170.     // text_tidy:
  171.     // Takes raw text from the DB and makes it all nice and pretty - which also
  172.     // parses un-HTML'd characters. Use this with caution!         
  173.     /*-------------------------------------------------------------------------*/
  174.     
  175.     function text_tidy($txt = "") {
  176.     
  177.         $trans = get_html_translation_table(HTML_ENTITIES);
  178.         $trans = array_flip($trans);
  179.         
  180.         $txt = strtr( $txt, $trans );
  181.         
  182.         $txt = preg_replace( "/\s{2}/" , "  "      , $txt );
  183.         $txt = preg_replace( "/\r/"    , "\n"           , $txt );
  184.         $txt = preg_replace( "/\t/"    , "  " , $txt );
  185.         //$txt = preg_replace( "/\\n/"   , "\n"       , $txt );
  186.         
  187.         return $txt;
  188.         
  189.     }
  190.  
  191.     /*-------------------------------------------------------------------------*/
  192.     // compile_db_string:
  193.     // Takes an array of keys and values and formats them into a string the DB
  194.     // can use.
  195.     // $array = ( 'THIS' => 'this', 'THAT' => 'that' );
  196.     // will be returned as THIS, THAT  'this', 'that'                
  197.     /*-------------------------------------------------------------------------*/
  198.     
  199.     function compile_db_string($data) {
  200.     
  201.         $field_names  = "";
  202.         $field_values = "";
  203.         
  204.         foreach ($data as $k => $v) {
  205.             $v = preg_replace( "/'/", "\\'", $v );
  206.             $field_names  .= "$k,";
  207.             $field_values .= "'$v',";
  208.         }
  209.         
  210.         $field_names  = preg_replace( "/,$/" , "" , $field_names  );
  211.         $field_values = preg_replace( "/,$/" , "" , $field_values );
  212.         
  213.         return array( 'FIELD_NAMES'  => $field_names,
  214.                       'FIELD_VALUES' => $field_values,
  215.                     );
  216.     }
  217.  
  218.  
  219.  
  220.     /*-------------------------------------------------------------------------*/
  221.     // Build up page span links                
  222.     /*-------------------------------------------------------------------------*/
  223.     
  224.     function build_pagelinks($data) {
  225.  
  226.         $work = array();
  227.     
  228.         $work['pages']        = 1;
  229.         
  230.         if ( ($data['TOTAL_POSS'] % $data['PER_PAGE']) == 0 ) {
  231.             $work['pages'] = $data['TOTAL_POSS'] / $data['PER_PAGE'];
  232.         } else {
  233.             $number = ($data['TOTAL_POSS'] / $data['PER_PAGE']);
  234.             $work['pages'] = ceil( $number);
  235.         }
  236.         
  237.         
  238.         $work['total_page']   = $work['pages'];
  239.         $work['current_page'] = $data['CUR_ST_VAL'] > 0 ? ($data['CUR_ST_VAL'] / $data['PER_PAGE']) + 1 : 1;
  240.     
  241.         if ($work['pages'] > 1) {
  242.             $work['first_page'] = "{$data['L_MULTI']} ({$work['pages']}) <a href='{$data['BASE_URL']}&st=0'><</a>";
  243.             for( $i = 0; $i <= $work['pages'] - 1; ++$i ) {
  244.                 $RealNo = $i * $data['PER_PAGE'];
  245.                 $PageNo = $i+1;
  246.                 if ($RealNo == $data['CUR_ST_VAL']) {
  247.                     $work['page_span'] .= " <b>[{$PageNo}]</b>";
  248.                 } else {
  249.                     if ($PageNo < ($work['current_page'] - 5) and ($work['current_page'] >= 6))  {
  250.                         $work['st_dots'] = ' ...';
  251.                         continue;
  252.                     }
  253.                     $work['page_span'] .= " <a href='{$data['BASE_URL']}&st={$RealNo}'>{$PageNo}</a>";
  254.                     if ($PageNo >= ($work['current_page'] + 5)) {
  255.                         $work['end_dots'] = '... ';
  256.                         break;
  257.                     }
  258.                 }
  259.             }
  260.             $work['last_page'] = "<a href='{$data['BASE_URL']}&st=".($work['pages']-1) * $data['PER_PAGE']."'>></a>";
  261.             $work['return']    = $work['first_page'].$work['st_dots'].$work['page_span'].' '.$work['end_dots'].$work['last_page'];
  262.         } else {
  263.             $work['return']    = $data['L_SINGLE'];
  264.         }
  265.     
  266.         return $work['return'];
  267.     }
  268.     
  269.     
  270.     
  271.     /*-------------------------------------------------------------------------*/
  272.     // Build the forum jump menu               
  273.     /*-------------------------------------------------------------------------*/ 
  274.     
  275.     function build_forum_jump($html=1) {
  276.         global $INFO, $DB, $ibforums;
  277.         // $html = 0 means don't return the select html stuff
  278.         // $html = 1 means return the jump menu with select and option stuff
  279.         
  280.         $last_cat_id = -1;
  281.         
  282.         $DB->query("SELECT f.id as forum_id, f.parent_id, f.subwrap, f.name as forum_name, f.position, f.read_perms, c.id as cat_id, c.name from ibf_forums f, ibf_categories c where c.id=f.category ORDER BY c.position, f.position");
  283.         
  284.         if ($html == 1) {
  285.         
  286.             $the_html = "<form onSubmit=\"if(document.jumpmenu.f.value == -1){return false;}\" action='{$ibforums->vars['board_url']}/index.{$ibforums->vars['php_ext']}?s={$ibforums->session_id}&act=SF' method='GET' name='jumpmenu'>"
  287.                        ."<input type='hidden' name='act' value='SF'>\n<input type='hidden' name='s' value='{$ibforums->session_id}'>"
  288.                        ."<select name='f' onChange=\"if(this.options[this.selectedIndex].value != -1){ document.jumpmenu.submit() }\" class='forminput'>"
  289.                        ."<option value='-1'>#Forum Jump#"
  290.                        ."<option value='-1'>------------";
  291.         }
  292.         
  293.         $forum_keys = array();
  294.         $cat_keys   = array();
  295.         $children   = array();
  296.         $subs       = array();
  297.             
  298.         while ( $i = $DB->fetch_row() )
  299.         {
  300.             $selected = '';
  301.         
  302.             if ($html == 1)
  303.             {
  304.                 if ($ibforums->input['f'] and $ibforums->input['f'] == $i['forum_id'])
  305.                 {
  306.                     $selected = ' selected';
  307.                 }
  308.             }
  309.             
  310.             if ($i['subwrap'] == 1)
  311.             {
  312.                 $forum_keys[ $i['cat_id'] ][$i['forum_id']] = "<option value=\"{$i['forum_id']}\"".$selected.">  -- {$i['forum_name']}</option>\n";
  313.             }
  314.             else
  315.             {
  316.                 if ($i['read_perms'] == '*')
  317.                 {
  318.                     if ($i['parent_id'] > 0)
  319.                     {
  320.                         $children[ $i['parent_id'] ][] = "<option value=\"{$i['forum_id']}\"".$selected.">  ---- {$i['forum_name']}</option>\n";
  321.                     }
  322.                     else
  323.                     {
  324.                         $forum_keys[ $i['cat_id'] ][$i['forum_id']] = "<option value=\"{$i['forum_id']}\"".$selected.">  - {$i['forum_name']}</option>\n";
  325.                     }
  326.                 }
  327.                 else if (preg_match( "/(^|,)".$ibforums->member[mgroup]."(,|$)/", $i['read_perms']) )
  328.                 {
  329.                     if ($i['parent_id'] > 0)
  330.                     {
  331.                         $children[ $i['parent_id'] ][] = "<option value=\"{$i['forum_id']}\"".$selected.">  ---- {$i['forum_name']}</option>\n";
  332.                     }
  333.                     else
  334.                     {
  335.                         $forum_keys[ $i['cat_id'] ][$i['forum_id']] = "<option value=\"{$i['forum_id']}\"".$selected.">  - {$i['forum_name']}</option>\n";
  336.                     }
  337.                 }
  338.                 else
  339.                 {
  340.                     continue;
  341.                 }
  342.             }
  343.             
  344.             if ($last_cat_id != $i['cat_id'])
  345.             {
  346.                 
  347.                 // Make sure cats with hidden forums are not shown in forum jump
  348.                 
  349.                 $cat_keys[ $i['cat_id'] ] = "<option value='-1'>{$i['name']}</option>\n";
  350.                                           
  351.                 $last_cat_id = $i['cat_id'];
  352.                 
  353.             }
  354.         }
  355.         
  356.         foreach($cat_keys as $cat_id => $cat_text)
  357.         {
  358.             if ( is_array( $forum_keys[$cat_id] ) && count( $forum_keys[$cat_id] ) > 0 )
  359.             {
  360.                 $the_html .= $cat_text;
  361.                 
  362.                 foreach($forum_keys[$cat_id] as $idx => $forum_text)
  363.                 {
  364.                     $the_html .= $forum_text;
  365.                     
  366.                     if (count($children[$idx]) > 0)
  367.                     {
  368.                         $the_html .= $t;
  369.                         
  370.                         foreach($children[$idx] as $ii => $tt)
  371.                         {
  372.                             $the_html .= $tt;
  373.                         }
  374.                     }
  375.                 }
  376.             }
  377.         }
  378.             
  379.         
  380.         if ($html == 1)
  381.         {
  382.             $the_html .= "</select> <input type='submit' value='{$ibforums->lang['jmp_go']}' class='forminput'></form>";
  383.         }
  384.         
  385.         return $the_html;
  386.         
  387.     }
  388.     
  389.     function clean_email($email = "") {
  390.  
  391.         $email = preg_replace( "#[\n\r\*\'\"<>&\%\!\(\)\{\}\[\]\?\\/]#", "", $email );
  392.         
  393.         if ( preg_match( "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/", $email) )
  394.         {
  395.             return $email;
  396.         }
  397.         else
  398.         {
  399.             return FALSE;
  400.         }
  401.     }
  402.     
  403.     
  404.     /*-------------------------------------------------------------------------*/
  405.     // SKIN, sort out the skin stuff                 
  406.     /*-------------------------------------------------------------------------*/
  407.     
  408.     function load_skin() {
  409.         global $ibforums, $INFO, $DB;
  410.         
  411.         $id       = -1;
  412.         $skin_set = 0;
  413.         
  414.         //------------------------------------------------
  415.         // Do we have a skin for a particular forum?
  416.         //------------------------------------------------
  417.         
  418.         if ($ibforums->input['f'] and $ibforums->input['act'] != 'UserCP')
  419.         {
  420.             if ( $ibforums->vars[ 'forum_skin_'.$ibforums->input['f'] ] != "" )
  421.             {
  422.                 $id = $ibforums->vars[ 'forum_skin_'.$ibforums->input['f'] ];
  423.                 
  424.                 $skin_set = 1;
  425.             }
  426.         }
  427.         
  428.         //------------------------------------------------
  429.         // Are we allowing user chooseable skins?
  430.         //------------------------------------------------
  431.         
  432.         $extra = "";
  433.         
  434.         if ($skin_set != 1 and $ibforums->vars['allow_skins'] == 1)
  435.         {
  436.             if (isset($ibforums->input['skinid']))
  437.             {
  438.                 $id    = $ibforums->input['skinid'];
  439.                 $extra = " AND s.hidden=0";
  440.                 $skin_set = 1;
  441.             }
  442.             else if ( $ibforums->member['skin'] != "" and intval($ibforums->member['skin']) >= 0 )
  443.             {
  444.                 $id = $ibforums->member['skin'];
  445.                 
  446.                 if ($id == 'Default') $id = -1;
  447.                 
  448.                 $skin_set = 1;
  449.             }
  450.             
  451.         }
  452.         
  453.         //------------------------------------------------
  454.         // Load the info from the database.
  455.         //------------------------------------------------
  456.         
  457.         if ( $id >= 0 and $skin_set == 1)
  458.         {
  459.         
  460.             $DB->query("SELECT s.*, i.*, t.template FROM ibf_templates t, ibf_skins s, ibf_images i ".
  461.                           "WHERE s.sid='$id' AND t.tmid=s.tmpl_id AND i.imid=s.img_id".$extra);
  462.                           
  463.             // Didn't get a row?
  464.             
  465.             if (! $DB->get_num_rows() )
  466.             {
  467.                 // Update this members profile
  468.                 
  469.                 if ( $ibforums->member['id'] )
  470.                 {
  471.                     $DB->query("UPDATE ibf_members SET skin='-1' WHERE id='".$ibforums->member['id']."'");
  472.                 }
  473.                 
  474.                 $DB->query("SELECT s.*, i.*, t.template FROM ibf_templates t, ibf_skins s, ibf_images i ".
  475.                               "WHERE s.default_set=1 AND t.tmid=s.tmpl_id AND i.imid=s.img_id");
  476.             }
  477.             
  478.         }
  479.         else
  480.         {
  481.             $DB->query("SELECT s.*, i.*, t.template FROM ibf_templates t, ibf_skins s, ibf_images i ".
  482.                           "WHERE s.default_set=1 AND t.tmid=s.tmpl_id AND i.imid=s.img_id");
  483.         }
  484.         
  485.         if ( ! $row = $DB->fetch_row() )
  486.         {
  487.             echo("Could not query the skin information!");
  488.             exit();
  489.         }
  490.         
  491.         return $row;
  492.         
  493.     }
  494.     
  495.     /*-------------------------------------------------------------------------*/
  496.     // Require, parse and return an array containing the language stuff                 
  497.     /*-------------------------------------------------------------------------*/ 
  498.     
  499.     function load_words($current_lang_array, $area, $lang_type) {
  500.     
  501.         require "./lang/".$lang_type."/".$area.".php";
  502.         
  503.         foreach ($lang as $k => $v)
  504.         {
  505.             $current_lang_array[$k] = stripslashes($v);
  506.         }
  507.         
  508.         unset($lang);
  509.         
  510.         return $current_lang_array;
  511.  
  512.     }
  513.  
  514.     
  515.     /*-------------------------------------------------------------------------*/
  516.     // Return a date or '--' if the date is undef.
  517.     // We use the rather nice gmdate function in PHP to synchronise our times
  518.     // with GMT. This gives us the following choices:
  519.     //
  520.     // If the user has specified a time offset, we use that. If they haven't set
  521.     // a time zone, we use the default board time offset (which should automagically
  522.     // be adjusted to match gmdate.             
  523.     /*-------------------------------------------------------------------------*/    
  524.     
  525.     function get_date($date, $method) {
  526.         global $ibforums;
  527.         
  528.         if (!$date)
  529.         {
  530.             return '--';
  531.         }
  532.         
  533.         if (empty($method))
  534.         {
  535.             $method = 'LONG';
  536.         }
  537.         
  538.         if ($this->offset_set == 0)
  539.         {
  540.             // Save redoing this code for each call, only do once per page load
  541.             
  542.             $this->offset = (($ibforums->member['time_offset'] != "") ? $ibforums->member['time_offset'] : $ibforums->vars['time_offset']) * 3600;
  543.             
  544.             if ($ibforums->vars['time_adjust'] != "" and $ibforums->vars['time_adjust'] != 0)
  545.             {
  546.                 $this->offset += ($ibforums->vars['time_adjust'] * 60);
  547.             }
  548.             
  549.             if ($ibforums->member['dst_in_use'])
  550.             {
  551.                 $this->offset += 3600;
  552.             }
  553.             
  554.             $this->offset_set = 1;
  555.         }
  556.         
  557.         
  558.         return gmdate($this->time_options[$method], ($date + $this->offset) );
  559.     }
  560.     
  561.     /*-------------------------------------------------------------------------*/
  562.     // Sets a cookie, abstract layer allows us to do some checking, etc                
  563.     /*-------------------------------------------------------------------------*/    
  564.     
  565.     function my_setcookie($name, $value = "", $sticky = 1) {
  566.         global $INFO;
  567.         
  568.         $exipres = "";
  569.         
  570.         if ($sticky == 1)
  571.         {
  572.             $expires = time() + 60*60*24*365;
  573.         }
  574.  
  575.         $INFO['cookie_domain'] = $INFO['cookie_domain'] == "" ? ""  : $INFO['cookie_domain'];
  576.         $INFO['cookie_path']   = $INFO['cookie_path']   == "" ? "/" : $INFO['cookie_path'];
  577.         
  578.         $name = $INFO['cookie_id'].$name;
  579.       
  580.         @setcookie($name, urlencode($value), $expires, $INFO['cookie_path'], $INFO['cookie_domain']);
  581.     }
  582.     
  583.     /*-------------------------------------------------------------------------*/
  584.     // Cookies, cookies everywhere and not a byte to eat.                
  585.     /*-------------------------------------------------------------------------*/  
  586.     
  587.     function my_getcookie($name)
  588.     {
  589.         global $INFO, $HTTP_COOKIE_VARS;
  590.         
  591.         if (isset($HTTP_COOKIE_VARS[$INFO['cookie_id'].$name]))
  592.         {
  593.             return urldecode($HTTP_COOKIE_VARS[$INFO['cookie_id'].$name]);
  594.         }
  595.         else
  596.         {
  597.             return FALSE;
  598.         }
  599.         
  600.     }
  601.     
  602.     /*-------------------------------------------------------------------------*/
  603.     // Makes incoming info "safe"              
  604.     /*-------------------------------------------------------------------------*/
  605.     
  606.     function parse_incoming()
  607.     {
  608.         global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_CLIENT_IP, $REQUEST_METHOD, $REMOTE_ADDR, $HTTP_PROXY_USER, $HTTP_X_FORWARDED_FOR;
  609.         $return = array();
  610.         
  611.         if( is_array($HTTP_GET_VARS) )
  612.         {
  613.             while( list($k, $v) = each($HTTP_GET_VARS) )
  614.             {
  615.                 //$k = $this->clean_key($k);
  616.                 if( is_array($HTTP_GET_VARS[$k]) )
  617.                 {
  618.                     while( list($k2, $v2) = each($HTTP_GET_VARS[$k]) )
  619.                     {
  620.                         $return[$k][ $this->clean_key($k2) ] = $this->clean_value($v2);
  621.                     }
  622.                 }
  623.                 else
  624.                 {
  625.                     $return[$k] = $this->clean_value($v);
  626.                 }
  627.             }
  628.         }
  629.         
  630.         // Overwrite GET data with post data
  631.         
  632.         if( is_array($HTTP_POST_VARS) )
  633.         {
  634.             while( list($k, $v) = each($HTTP_POST_VARS) )
  635.             {
  636.                 //$k = $this->clean_key($k);
  637.                 if ( is_array($HTTP_POST_VARS[$k]) )
  638.                 {
  639.                     while( list($k2, $v2) = each($HTTP_POST_VARS[$k]) )
  640.                     {
  641.                         $return[$k][ $this->clean_key($k2) ] = $this->clean_value($v2);
  642.                     }
  643.                 }
  644.                 else
  645.                 {
  646.                     $return[$k] = $this->clean_value($v);
  647.                 }
  648.             }
  649.         }
  650.         
  651.         // Sort out the accessing IP
  652.         
  653.         $return['IP_ADDRESS'] = $this->select_var( array( 
  654.                                                           1 => $HTTP_X_FORWARDED_FOR,
  655.                                                           2 => $HTTP_PROXY_USER,
  656.                                                           3 => $REMOTE_ADDR,
  657.                                                           4 => $_SERVER['REMOTE_ADDR']
  658.                                                         )
  659.                                                  );
  660.                                                  
  661.         // Make sure we take a valid IP address
  662.         
  663.         $return['IP_ADDRESS'] = preg_replace( "/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/", "\\1.\\2.\\3.\\4", $return['IP_ADDRESS'] );
  664.         
  665.         $return['request_method'] = strtolower($REQUEST_METHOD);
  666.         
  667.         
  668.         return $return;
  669.     }
  670.     
  671.     /*-------------------------------------------------------------------------*/
  672.     // Key Cleaner - ensures no funny business with form elements             
  673.     /*-------------------------------------------------------------------------*/
  674.     
  675.     function clean_key($key) {
  676.     
  677.         if ($key == "")
  678.         {
  679.             return "";
  680.         }
  681.         $key = preg_replace( "/\.\./"           , ""  , $key );
  682.         $key = preg_replace( "/\_\_(.+?)\_\_/"  , ""  , $key );
  683.         $key = preg_replace( "/^([\w\.\-\_]+)$/", "$1", $key );
  684.         return $key;
  685.     }
  686.     
  687.     function clean_value($val) {
  688.     
  689.         if ($val == "")
  690.         {
  691.             return "";
  692.         }
  693.         $val = preg_replace( "/&/"         , "&"         , $val );
  694.         $val = preg_replace( "/<!--/"      , "<!--"  , $val );
  695.         $val = preg_replace( "/-->/"       , "-->"       , $val );
  696.         $val = preg_replace( "/<script/i"  , "<script"   , $val );
  697.         $val = preg_replace( "/>/"         , ">"          , $val );
  698.         $val = preg_replace( "/</"         , "<"          , $val );
  699.         $val = preg_replace( "/\"/"        , """        , $val );
  700.         $val = preg_replace( "/\|/"        , "|"        , $val );
  701.         $val = preg_replace( "/\n/"        , "<br>"          , $val ); // Convert literal newlines
  702.         $val = preg_replace( "/\\\$/"      , "$"        , $val );
  703.         $val = preg_replace( "/\r/"        , ""              , $val ); // Remove literal carriage returns
  704.         $val = preg_replace( "/!/"         , "!"         , $val );
  705.         $val = preg_replace( "/'/"         , "'"         , $val ); // IMPORTANT: It helps to increase sql query safety.
  706.         $val = stripslashes($val);                                     // Swop PHP added backslashes
  707.         $val = preg_replace( "/\\\/"       , "\"        , $val ); // Swop user inputted backslashes
  708.         return $val;
  709.     }
  710.     
  711.     
  712.     function is_number($number="")
  713.     {
  714.     
  715.         if ($number == "") return -1;
  716.         
  717.         if ( preg_match( "/^([0-9]+)$/", $number ) )
  718.         {
  719.             return $number;
  720.         }
  721.         else
  722.         {
  723.             return "";
  724.         }
  725.     }
  726.     
  727.     /*-------------------------------------------------------------------------*/
  728.     // MEMBER FUNCTIONS             
  729.     /*-------------------------------------------------------------------------*/
  730.     
  731.     
  732.     function set_up_guest($name='Guest') {
  733.         global $INFO;
  734.     
  735.         return array( 'name'     => $name,
  736.                       'id'       => 0,
  737.                       'password' => "",
  738.                       'email'    => "",
  739.                       'title'    => "Unregistered",
  740.                       'mgroup'    => $INFO['guest_group'],
  741.                       'view_sigs' => $INFO['guests_sig'],
  742.                       'view_img'  => $INFO['guests_img'],
  743.                       'view_avs'  => $INFO['guests_ava'],
  744.                     );
  745.     }
  746.     
  747.     /*-------------------------------------------------------------------------*/
  748.     // GET USER AVATAR         
  749.     /*-------------------------------------------------------------------------*/
  750.     
  751.     function get_avatar($member_avatar="", $member_view_avatars=0, $avatar_dims="x") {
  752.         global $ibforums;
  753.         
  754.         if (!$member_avatar or $member_view_avatars == 0 or !$ibforums->vars['avatars_on'])
  755.         {
  756.             return "";
  757.         }
  758.         
  759.         if (preg_match ( "/^noavatar/", $member_avatar ))
  760.         {
  761.             return "";
  762.         }
  763.         
  764.         if ( (preg_match ( "/\.swf/", $member_avatar)) and ($ibforums->vars['allow_flash'] != 1) )
  765.         {
  766.             return "";
  767.         }
  768.         
  769.         $davatar_dims    = explode( "x", $ibforums->vars['avatar_dims'] );
  770.         $default_a_dims  = explode( "x", $ibforums->vars['avatar_def'] );
  771.         
  772.         
  773.          // Have we enabled URL / Upload avatars?
  774.      
  775.          $this_dims = explode( "x", $avatar_dims );
  776.          if (!$this_dims[0]) $this_dims[0] = $davatar_dims[0];
  777.          if (!$this_dims[1]) $this_dims[1] = $davatar_dims[1];
  778.              
  779.          if ( preg_match( "/^http:\/\//", $member_avatar ) )
  780.          {
  781.              // Ok, it's a URL..
  782.              
  783.              if (preg_match ( "/\.swf/", $member_avatar))
  784.              {
  785.                  return "<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" WIDTH={$this_dims[0]} HEIGHT={$this_dims[1]}><PARAM NAME=MOVIE VALUE={$member_avatar}><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><EMBED SRC={$member_avatar} WIDTH={$this_dims[0]} HEIGHT={$this_dims[1]} PLAY=TRUE LOOP=TRUE QUALITY=HIGH></EMBED></OBJECT>";
  786.              }
  787.              else
  788.              {
  789.                  return "<img src='{$member_avatar}' border='0' width='{$this_dims[0]}' height='{$this_dims[1]}'>";
  790.              }
  791.              
  792.              // Not a URL? Is it an uploaded avatar?
  793.          }
  794.          else if ( ($ibforums->vars['avup_size_max'] > 1) and ( preg_match( "/^upload:av-(?:\d+)\.(?:\S+)/", $member_avatar ) ) )
  795.          {
  796.              
  797.              $member_avatar = preg_replace( "/^upload:/", "", $member_avatar );
  798.              
  799.              return "<img src='{$ibforums->vars['upload_url']}/$member_avatar' border='0' width='{$this_dims[0]}' height='{$this_dims[1]}'>";
  800.          }
  801.          // No, it's not a URL or an upload, must be a normal avatar then
  802.          else if ($member_avatar != "")
  803.          {
  804.              // Do we have an avatar still ?
  805.              
  806.              return "<img src='{$ibforums->vars['AVATARS_URL']}/{$member_avatar}' border='0' width='{$default_a_dims[0]}' height='{$default_a_dims[1]}'>";
  807.          }
  808.          else
  809.          {
  810.              // No, ok - return blank
  811.              return "";
  812.          }
  813.     }
  814.  
  815.  
  816.  
  817.  
  818.     /*-------------------------------------------------------------------------*/
  819.     // ERROR FUNCTIONS             
  820.     /*-------------------------------------------------------------------------*/
  821.     
  822.     function Error($error) {
  823.         global $DB, $ibforums, $root_path, $skin_universal, $QUERY_STRING;
  824.         
  825.         
  826.         if ( $error['MSG'] == 'server_too_busy' or $error['MSG'] == 'you_are_banned')
  827.         {
  828.             
  829.             $DB->query("SELECT s.*, i.*, t.template FROM ibf_templates t, ibf_skins s, ibf_images i ".
  830.                           "WHERE s.default_set=1 AND t.tmid=s.tmpl_id AND i.imid=s.img_id");
  831.                           
  832.             $ibforums->skin = $DB->fetch_row();
  833.                           
  834.             require $root_path."Skin/s".$ibforums->skin['set_id']."/skin_global.php";
  835.             
  836.             $ibforums->session_id = $this->my_getcookie('session_id');
  837.  
  838.             $skin_universal = new skin_global();
  839.             
  840.             $ibforums->base_url   = $ibforums->vars['board_url'].'/index.'.$ibforums->vars['php_ext'].'?s='.$ibforums->session_id;
  841.             $ibforums->vars['img_url']   = 'style_images/' . $ibforums->skin['img_id'];
  842.  
  843.         }
  844.  
  845.         $ibforums->lang = $this->load_words($ibforums->lang, "lang_error", $ibforums->lang_id);
  846.         
  847.         list($em_1, $em_2) = explode( '@', $ibforums->vars['email_in'] );
  848.         
  849.         $msg = $ibforums->lang[ $error['MSG'] ];
  850.         
  851.         if ($error['EXTRA'])
  852.         {
  853.             $msg = preg_replace( "/<#EXTRA#>/", $error['EXTRA'], $msg );
  854.         }
  855.         
  856.         $html = $skin_universal->Error( $msg, $em_1, $em_2);
  857.         
  858.         // If we're a guest, show the log in box..
  859.         
  860.         if ($ibforums->member['id'] == "" and $error['MSG'] != 'server_too_busy')
  861.         {
  862.             $html = preg_replace( "/<!-- IBF\.LOG_IN_TABLE -->/e", "\$skin_universal->error_log_in(\$QUERY_STRING)", $html);
  863.         }
  864.         
  865.         $print = new display();
  866.         
  867.         $print->add_output($html);
  868.             
  869.         $print->do_output( array(
  870.                                     OVERRIDE   => 1,
  871.                                     TITLE      => $ibforums->lang['error_title'],
  872.                                  )
  873.                           );
  874.     }
  875.     
  876.     function board_offline()
  877.     {
  878.         global $DB, $ibforums, $root_path, $skin_universal;
  879.         
  880.         $ibforums->lang = $this->load_words($ibforums->lang, "lang_error", $ibforums->lang_id);
  881.         
  882.         $msg = preg_replace( "/\n/", "<br>", stripslashes($ibforums->vars['offline_msg']) );
  883.         
  884.         $html = $skin_universal->board_offline( $msg );
  885.         
  886.         $print = new display();
  887.         
  888.         $print->add_output($html);
  889.             
  890.         $print->do_output( array(
  891.                                     OVERRIDE   => 1,
  892.                                     TITLE      => $ibforums->lang['offline_title'],
  893.                                  )
  894.                           );
  895.     }
  896.                                     
  897.     /*-------------------------------------------------------------------------*/
  898.     // Variable chooser             
  899.     /*-------------------------------------------------------------------------*/
  900.     
  901.     function select_var($array) {
  902.         
  903.         if ( !is_array($array) ) return -1;
  904.         
  905.         ksort($array);
  906.         
  907.         
  908.         $chosen = -1;  // Ensure that we return zero if nothing else is available
  909.         
  910.         foreach ($array as $k => $v)
  911.         {
  912.             if (isset($v))
  913.             {
  914.                 $chosen = $v;
  915.                 break;
  916.             }
  917.         }
  918.         
  919.         return $chosen;
  920.     }
  921.       
  922.     
  923. } // end class
  924.  
  925.  
  926. //######################################################
  927. // Our "print" class
  928. //######################################################
  929.  
  930.  
  931. class display {
  932.  
  933.     var $to_print = "";
  934.     
  935.     //-------------------------------------------
  936.     // Appends the parsed HTML to our class var
  937.     //-------------------------------------------
  938.     
  939.     function add_output($to_add) {
  940.         $this->to_print .= $to_add;
  941.         //return 'true' on success
  942.         return 'true';
  943.     }
  944.     
  945.     //-------------------------------------------
  946.     // Parses all the information and prints it.
  947.     //-------------------------------------------
  948.     
  949.     function do_output($output_array) {
  950.         global $DB, $Debug, $skin_universal, $ibforums;
  951.         
  952.         $ex_time     = sprintf( "%.4f",$Debug->endTimer() );
  953.         
  954.         $query_cnt   = $DB->get_query_cnt();
  955.         
  956.         $input   = "";
  957.         $queries = "";
  958.         $sload   = "";
  959.         
  960.         $gzip_status = $ibforums->vars['disable_gzip'] == 1 ? $ibforums->lang['gzip_off'] : $ibforums->lang['gzip_on'];
  961.         
  962.         if ($ibforums->server_load > 0)
  963.         {
  964.             $sload = '  [ Server Load: '.$ibforums->server_load.' ]';
  965.         }
  966.         
  967.         //+----------------------------------------------
  968.         
  969.         if ($ibforums->vars['debug_level'] > 0)
  970.         {
  971.         
  972.             $stats = "<br><table width='{$ibforums->skin['tbl_width']}' cellpadding='4' align='center' cellspacing='0' id='row1'>
  973.                        <tr>
  974.                          <td align='center'>[ Script Execution time: $ex_time ]   [ $query_cnt queries used ]   [ $gzip_status ] $sload</td>
  975.                        </tr>
  976.                       </table>";
  977.         }
  978.                   
  979.        //+----------------------------------------------
  980.                   
  981.        if ($ibforums->vars['debug_level'] >= 2)
  982.        {
  983.                $stats .= "<br><table width='{$ibforums->skin['tbl_width']}' align='center' cellpadding='0' cellspacing='1' bgcolor='{$ibforums->skin['tbl_border']}'>
  984.                            <tr>
  985.                             <td>
  986.                              <table width='100%' align='center' cellpadding='4' cellspacing='1'>
  987.                            <tr>
  988.                              <td colspan='2' id='titlemedium' align='center'>FORM and GET Input</td>
  989.                            </tr>";
  990.         
  991.             while( list($k, $v) = each($ibforums->input) )
  992.             {
  993.                 $stats .= "<tr><td width='20%' id='row1'>$k</td><td width='80%' id='row1'>$v</td></tr>";
  994.             }
  995.             
  996.             $stats .= "</table></td></tr></table>";
  997.         
  998.         }
  999.         
  1000.         //+----------------------------------------------
  1001.         
  1002.         if ($ibforums->vars['debug_level'] >= 3)
  1003.         {
  1004.             $stats .= "<br><table width='{$ibforums->skin['tbl_width']}' align='center' cellpadding='0' cellspacing='1' bgcolor='{$ibforums->skin['tbl_border']}'>
  1005.                            <tr>
  1006.                             <td>
  1007.                              <table width='100%' align='center' cellpadding='4' cellspacing='1'>
  1008.                            <tr>
  1009.                              <td colspan='2' id='titlemedium' align='center'>Queries Used</td>
  1010.                            </tr>";
  1011.                            
  1012.             foreach($DB->obj['cached_queries'] as $q)
  1013.             {
  1014.                 $q = preg_replace( "/^SELECT/i" , "<font style='color:red;font-weight:bold'>SELECT</font>"   , $q );
  1015.                 $q = preg_replace( "/^UPDATE/i" , "<font style='color:blue;font-weight:bold'>UPDATE</font>"  , $q );
  1016.                 $q = preg_replace( "/^DELETE/i" , "<font style='color:orange;font-weight:bold'>DELETE</font>", $q );
  1017.                 $q = preg_replace( "/^INSERT/i" , "<font style='color:green;font-weight:bold'>INSERT</font>" , $q );
  1018.                 
  1019.                 $q = preg_replace( "/(".$ibforums->vars['sql_tbl_prefix'].")(\S+?)([\s\.,]|$)/", "<font style='color:purple;font-weight:bold'>\\1\\2</font>\\3", $q );
  1020.                 
  1021.                 $stats .= "<tr><td id='row1'>$q</td></tr>";
  1022.             }
  1023.             
  1024.             $stats .= "</table></td></tr></table>";
  1025.         }
  1026.  
  1027.         
  1028.         /********************************************************/
  1029.         // NAVIGATION
  1030.         
  1031.         $nav  = $skin_universal->start_nav();
  1032.         
  1033.         $admin_link = $ibforums->member['g_access_cp'] ? $skin_universal->admin_link() : '';
  1034.         
  1035.         $nav .= "<a href='{$ibforums->vars['board_url']}/index.{$ibforums->vars['php_ext']}?s={$ibforums->session_id}'>{$ibforums->vars['board_name']}</a> $admin_link";
  1036.         
  1037.         if ( empty($output_array['OVERRIDE']) )
  1038.         {
  1039.             if (is_array( $output_array['NAV'] ) )
  1040.             {
  1041.                 foreach ($output_array['NAV'] as $n)
  1042.                 {
  1043.                     if ($n)
  1044.                     {
  1045.                         $nav .= $ibforums->skin['F_NAV_SEP'] . $n;
  1046.                     }
  1047.                 }
  1048.             }
  1049.         }
  1050.         
  1051.         $nav .= $skin_universal->end_nav();
  1052.         
  1053.         
  1054.      
  1055.         /********************************************************/
  1056.         // CSS
  1057.         
  1058.         $css = "\n<link rel='stylesheet' href='style_sheets/stylesheet_".$ibforums->skin['css_id'].".css' type='text/css'>\n";
  1059.         
  1060.         $copyright = "<!-- Copyright Information -->\n\n<p><table width='80%' align='center' cellpadding='3' cellspacing='0'><tr><td align='center' valign='middle' id='copyright'>$b_copy<br>Powered by <a href=\"http://www.invisionboard.com\" class=\"copyright\" target='_blank'>Invision Board</a> {$ibforums->version} © 2002  <a href='http://www.invisionpower.com' target='_blank'>Invision PS</a></td></tr></table><p>";
  1061.         
  1062.                        
  1063.         /********************************************************/
  1064.         // Build the board header
  1065.         
  1066.         $this_header  = $skin_universal->BoardHeader();
  1067.         
  1068.         // Build the members bar
  1069.  
  1070.         if ($ibforums->member['id'] == 0)
  1071.         {
  1072.             $output_array['MEMBER_BAR'] = $skin_universal->Guest_bar();
  1073.         }
  1074.         else if (!$ibforums->member['g_use_pm'])
  1075.         {
  1076.             $output_array['MEMBER_BAR'] = $skin_universal->Member_no_usepm_bar();
  1077.         }
  1078.         else
  1079.         {
  1080.             $pm_js = "";
  1081.             
  1082.             if ( ($ibforums->vars['max_messages'] != "") and ($ibforums->member['msg_total'] >= $ibforums->vars['max_messages']) )
  1083.             {
  1084.                 $msg_data['TEXT'] = $ibforums->lang['msg_full'];
  1085.             }
  1086.             else
  1087.             {
  1088.                 $ibforums->member['new_msg'] = $ibforums->member['new_msg'] == "" ? 0 : $ibforums->member['new_msg'];
  1089.             
  1090.                 $msg_data['TEXT'] = sprintf( $ibforums->lang['msg_new'], $ibforums->member['new_msg']);
  1091.             }
  1092.             
  1093.             // Do we have a pop up to show?
  1094.             
  1095.             if ($ibforums->member['show_popup'])
  1096.             {
  1097.                 $DB->query("UPDATE ibf_members SET show_popup='0' WHERE id='{$ibforums->member['id']}'");
  1098.                 $pm_js = $skin_universal->PM_popup();
  1099.             }
  1100.         
  1101.             $output_array['MEMBER_BAR'] = $pm_js . $skin_universal->Member_bar($msg_data);
  1102.          }
  1103.          
  1104.          if ($ibforums->vars['board_offline'] == 1)
  1105.          {
  1106.              $output_array['TITLE'] = $ibforums->lang['warn_offline']." ".$output_array['TITLE'];
  1107.          }
  1108.         
  1109.         // Get the template
  1110.         
  1111.         $ibforums->skin['template'] = str_replace( "<% CSS %>"            , "$css"                   , $ibforums->skin['template']);
  1112.         $ibforums->skin['template'] = str_replace( "<% JAVASCRIPT %>"     , ""                       , $ibforums->skin['template']);
  1113.         $ibforums->skin['template'] = str_replace( "<% TITLE %>"          , $output_array['TITLE']   , $ibforums->skin['template']);
  1114.         $ibforums->skin['template'] = str_replace( "<% BOARD %>"          , $this->to_print          , $ibforums->skin['template']);
  1115.         $ibforums->skin['template'] = str_replace( "<% STATS %>"          , $stats                   , $ibforums->skin['template']);
  1116.         $ibforums->skin['template'] = str_replace( "<% GENERATOR %>"      , ""                       , $ibforums->skin['template']);
  1117.         $ibforums->skin['template'] = str_replace( "<% COPYRIGHT %>"      , $copyright               , $ibforums->skin['template']);
  1118.         $ibforums->skin['template'] = str_replace( "<% BOARD HEADER %>"   , $this_header             , $ibforums->skin['template']);
  1119.         $ibforums->skin['template'] = str_replace( "<% NAVIGATION %>"     , $nav                     , $ibforums->skin['template']);
  1120.         
  1121.         if ( empty($output_array['OVERRIDE']) )
  1122.         {
  1123.               $ibforums->skin['template'] = str_replace( "<% MEMBER BAR %>"     , $output_array['MEMBER_BAR'], $ibforums->skin['template']);
  1124.         }
  1125.         else
  1126.         {
  1127.               $ibforums->skin['template'] = str_replace( "<% MEMBER BAR %>"     , "<br>"                     , $ibforums->skin['template']);
  1128.           }
  1129.         
  1130.         
  1131.         
  1132.         // Close this DB connection
  1133.         
  1134.         $DB->close_db();
  1135.         
  1136.         // Start GZIP compression
  1137.         
  1138.         if ($ibforums->vars['disable_gzip'] != 1)
  1139.         {
  1140.             ob_start ('ob_gzhandler');
  1141.         }
  1142.         
  1143.         $this->do_headers();
  1144.         
  1145.         print $ibforums->skin['template'];
  1146.         
  1147.         exit;
  1148.     }
  1149.     
  1150.     //-------------------------------------------
  1151.     // print the headers
  1152.     //-------------------------------------------
  1153.         
  1154.     function do_headers() {
  1155.         global $ibforums;
  1156.         
  1157.         if ($ibforums->vars['print_headers'])
  1158.         {
  1159.             @header("HTTP/1.0 200 OK");
  1160.             @header("HTTP/1.1 200 OK");
  1161.             @header("Content-type: text/html");
  1162.             
  1163.             if ($ibforums->vars['nocache'])
  1164.             {
  1165.                 @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  1166.                 @header("Cache-Control: no-cache, must-revalidate");
  1167.                 @header("Pragma: no-cache");
  1168.             }
  1169.         }
  1170.     }
  1171.     
  1172.     //-------------------------------------------
  1173.     // print a pure redirect screen
  1174.     //-------------------------------------------
  1175.     
  1176.     
  1177.     function redirect_screen($text="", $url="") {
  1178.         global $ibforums, $skin_universal, $DB;
  1179.         
  1180.         $url = $start . "?s={$ibforums->session_id}&".$url;
  1181.         
  1182.         $ibforums->lang['stand_by'] = stripslashes($ibforums->lang['stand_by']);
  1183.         
  1184.         $htm = $skin_universal->Redirect($text, $url);
  1185.         
  1186.         // Close this DB connection
  1187.         
  1188.         $DB->close_db();
  1189.         
  1190.         // Start GZIP compression
  1191.         
  1192.         if ($ibforums->vars['disable_gzip'] != 1)
  1193.         {
  1194.             ob_start ('ob_gzhandler');
  1195.         }
  1196.         
  1197.         $this->do_headers();
  1198.         
  1199.         echo ($htm);
  1200.         exit;
  1201.     }
  1202.     
  1203.     //-------------------------------------------
  1204.     // print a minimalist screen suitable for small
  1205.     // pop up windows
  1206.     //-------------------------------------------
  1207.     
  1208.     function pop_up_window($title = 'IBForums', $text = "" ) {
  1209.         global $ibforums, $DB;
  1210.         
  1211.         $html = "<html>
  1212.                    <head>
  1213.                       <title>$title</title>
  1214.                       <link rel='stylesheet' href='style_sheets/stylesheet_{$ibforums->skin['css_id']}.css' type='text/css'>
  1215.                    </head>
  1216.                    <body topmargin='0' leftmargin='0' rightmargin='0' marginwidth='0' marginheight='0' alink='#000000' vlink='#000000'>
  1217.                    $text
  1218.                    </body>
  1219.                  </html>
  1220.                 ";
  1221.         
  1222.         $DB->close_db();
  1223.           
  1224.         if ($ibforums->vars['disable_gzip'] != 1)
  1225.         {
  1226.             ob_start ('ob_gzhandler');
  1227.         }
  1228.         
  1229.         $this->do_headers();
  1230.         
  1231.         echo ($html);
  1232.         exit;
  1233.     } 
  1234.     
  1235.     
  1236.     
  1237. } // END class
  1238.     
  1239.  
  1240.  
  1241.  
  1242. //######################################################
  1243. // Our "session" class
  1244. //######################################################
  1245.  
  1246.  
  1247. class session {
  1248.  
  1249.     var $ip_address = 0;
  1250.     var $user_agent = "";
  1251.     var $time_now   = 0;
  1252.     var $session_id = 0;
  1253.     var $session_dead_id = 0;
  1254.     var $session_user_id = 0;
  1255.     var $session_user_pass = "";
  1256.     var $last_click        = 0;
  1257.     var $location          = "";
  1258.     var $member            = array();
  1259.  
  1260.     // No need for a constructor
  1261.     
  1262.     function authorise() {
  1263.         global $DB, $INFO, $ibforums, $std, $HTTP_USER_AGENT;
  1264.         
  1265.         //-------------------------------------------------
  1266.         // Before we go any lets check the load settings..
  1267.         //-------------------------------------------------
  1268.         
  1269.         if ($ibforums->vars['load_limit'] > 0)
  1270.         {
  1271.             if ( file_exists('/proc/loadavg') )
  1272.             {
  1273.                 if ( $fh = @fopen( '/proc/loadavg', 'r' ) )
  1274.                 {
  1275.                     $data = @fread( $fh, 6 );
  1276.                     @fclose( $fh );
  1277.                     
  1278.                     $load_avg = explode( " ", $data );
  1279.                     
  1280.                     $ibforums->server_load = trim($load_avg[0]);
  1281.                     
  1282.                     if ($ibforums->server_load > $ibforums->vars['load_limit'])
  1283.                     {
  1284.                         $std->Error( array( 'LEVEL' => 1, 'MSG' => 'server_too_busy' ) );
  1285.                     }
  1286.                 }
  1287.             }
  1288.         }
  1289.         
  1290.         //--------------------------------------------
  1291.         // Are they banned?
  1292.         //--------------------------------------------
  1293.         
  1294.         if ($ibforums->vars['ban_ip'])
  1295.         {
  1296.             $ips = explode( "|", $ibforums->vars['ban_ip'] );
  1297.             
  1298.             foreach ($ips as $ip)
  1299.             {
  1300.                 $ip = preg_replace( "/\*/", '.*' , $ip );
  1301.                 if (preg_match( "/$ip/", $ibforums->input['IP_ADDRESS'] ))
  1302.                 {
  1303.                     $std->Error( array( LEVEL => 1, MSG => 'you_are_banned' ) );
  1304.                 }
  1305.             }
  1306.         }
  1307.         
  1308.         //--------------------------------------------
  1309.         
  1310.         $this->member = array( 'id' => 0, 'password' => "", 'name' => "", 'mgroup' => $INFO['guest_group'] );
  1311.         
  1312.         //-------------------------------------------------
  1313.         // If we are accessing the registration functions,
  1314.         // lets not confuse things.
  1315.         //-------------------------------------------------
  1316.         
  1317.         // We don't want to check if we're registering and we don't want to start
  1318.         // any new headers if we're simply viewing an attachment..
  1319.         
  1320.         if ( $ibforums->input['act'] == 'Reg' or $ibforums->input['act'] == 'Attach' )
  1321.         {
  1322.             return $this->member;
  1323.         }
  1324.         
  1325.         $this->ip_address = $ibforums->input['IP_ADDRESS'];
  1326.         $this->user_agent = substr($HTTP_USER_AGENT,0,50);
  1327.         $this->time_now   = time();
  1328.         
  1329.         $cookie = array();
  1330.         $cookie['session_id']   = $std->my_getcookie('session_id');
  1331.         $cookie['member_id']    = $std->my_getcookie('member_id');
  1332.         $cookie['pass_hash']    = $std->my_getcookie('pass_hash');
  1333.         
  1334.        
  1335.         if (! empty($cookie['session_id']) )
  1336.         {
  1337.             $this->get_session($cookie['session_id']);
  1338.         }
  1339.         elseif (! empty($ibforums->input['s']) )
  1340.         {
  1341.             $this->get_session($ibforums->input['s']);
  1342.         }
  1343.         else
  1344.         {
  1345.             $this->session_id = 0;
  1346.         }
  1347.         
  1348.         //-------------------------------------------------
  1349.         // Finalise the incoming data..
  1350.         //-------------------------------------------------
  1351.         
  1352.         $ibforums->input['Privacy'] = $std->select_var( array( 
  1353.                                                                1 => $ibforums->input['Privacy'],
  1354.                                                                2 => $std->my_getcookie('anonlogin')
  1355.                                                       )      );
  1356.                                                       
  1357.         //-------------------------------------------------                                  
  1358.         // Do we have a valid session ID?
  1359.         //-------------------------------------------------
  1360.         
  1361.         if ( ($this->session_id != 0) and ( ! empty($this->session_id) ) )
  1362.         {
  1363.             // We've checked the IP addy and browser, so we can assume that this is
  1364.             // a valid session.
  1365.             
  1366.             if ( ($this->session_user_id != 0) and ( ! empty($this->session_user_id) ) )
  1367.             {
  1368.                 // It's a member session, so load the member.
  1369.                 
  1370.                 $this->load_member($this->session_user_id);
  1371.                 
  1372.                 // Did we get a member?
  1373.                 
  1374.                 if ( (! $this->member['id']) or ($this->member['id'] == 0) )
  1375.                 {
  1376.                     $this->unload_member();
  1377.                     $this->update_guest_session();
  1378.                 }
  1379.                 else
  1380.                 {
  1381.                     $this->update_member_session();
  1382.                 }
  1383.             }
  1384.             else
  1385.             {
  1386.                 $this->update_guest_session();
  1387.             }
  1388.         
  1389.         }
  1390.         else
  1391.         {
  1392.             // We didn't have a session, or the session didn't validate
  1393.             
  1394.             // Do we have cookies stored?
  1395.             
  1396.             if ($cookie['member_id'] != "" and $cookie['pass_hash'] != "")
  1397.             {
  1398.                 $this->load_member($cookie['member_id']);
  1399.                 
  1400.                 if ( (! $this->member['id']) or ($this->member['id'] == 0) )
  1401.                 {
  1402.                     $this->unload_member();
  1403.                     $this->create_guest_session();
  1404.                 }
  1405.                 else
  1406.                 {
  1407.                     if ($this->member['password'] == $cookie['pass_hash'])
  1408.                     {
  1409.                         $this->create_member_session();
  1410.                     }
  1411.                     else
  1412.                     {
  1413.                         $this->unload_member();
  1414.                         $this->create_guest_session();
  1415.                     }
  1416.                 }
  1417.             }
  1418.             else
  1419.             {
  1420.                 $this->create_guest_session();
  1421.             }
  1422.         }
  1423.         
  1424.         //-------------------------------------------------
  1425.         // Set up a guest if we get here and we don't have a member ID
  1426.         //-------------------------------------------------
  1427.         
  1428.         if (! $this->member['id'])
  1429.         {
  1430.             $this->member = $std->set_up_guest();
  1431.             $DB->query("SELECT * from ibf_groups WHERE g_id='".$INFO['guest_group']."'");
  1432.             $group = $DB->fetch_row();
  1433.         
  1434.             foreach ($group as $k => $v)
  1435.             {
  1436.                 $this->member[ $k ] = $v;
  1437.             }
  1438.         
  1439.         }
  1440.         
  1441.         //------------------------------------------------
  1442.         // Synchronise the last visit and activity times if
  1443.         // we have some in the member profile
  1444.         //-------------------------------------------------
  1445.         
  1446.         if ($this->member['id'])
  1447.         {
  1448.             if ( ! $ibforums->input['last_activity'] )
  1449.             {
  1450.                 if ($this->member['last_activity'])
  1451.                 {
  1452.                     $ibforums->input['last_activity'] = $this->member['last_activity'];
  1453.                 }
  1454.                 else
  1455.                 {
  1456.                     $ibforums->input['last_activity'] = $this->time_now;
  1457.                 }
  1458.             }
  1459.             //------------
  1460.             
  1461.             if ( ! $ibforums->input['last_visit'] )
  1462.             {
  1463.                 if ($this->member['last_visit'])
  1464.                 {
  1465.                     $ibforums->input['last_visit'] = $this->member['last_visit'];
  1466.                 }
  1467.                 else
  1468.                 {
  1469.                     $ibforums->input['last_visit'] = $this->time_now;
  1470.                 }
  1471.             }
  1472.         
  1473.             //-------------------------------------------------
  1474.             // If there hasn't been a cookie update in 2 hours,
  1475.             // we assume that they've gone and come back
  1476.             //-------------------------------------------------
  1477.             
  1478.             if (!$this->member['last_visit'])
  1479.             {
  1480.                 // No last visit set, do so now!
  1481.                 
  1482.                 $DB->query("UPDATE ibf_members SET last_visit='".$this->time_now."', last_activity='".$this->time_now."' WHERE id='".$this->member['id']."'");
  1483.                 
  1484.             }
  1485.             else if ( (time() - $ibforums->input['last_activity']) > 300 )
  1486.             {
  1487.                 // If the last click was longer than 5 mins ago and this is a member
  1488.                 // Update their profile.
  1489.                 
  1490.                 $DB->query("UPDATE ibf_members SET last_activity='".$this->time_now."' WHERE id='".$this->member['id']."'");
  1491.                 
  1492.             }
  1493.         
  1494.         }
  1495.         
  1496.         //-------------------------------------------------
  1497.         // Set a session ID cookie
  1498.         //-------------------------------------------------
  1499.         
  1500.         $std->my_setcookie("session_id", $this->session_id, -1);
  1501.         
  1502.         return $this->member;
  1503.         
  1504.     }
  1505.     
  1506.     //+-------------------------------------------------
  1507.     // Attempt to load a member
  1508.     //+-------------------------------------------------
  1509.     
  1510.     function load_member($member_id=0)
  1511.     {
  1512.         global $DB, $std, $ibforums;
  1513.         
  1514.          if ($member_id != 0)
  1515.         {
  1516.             $DB->query("SELECT m.id, m.name, m.mgroup, m.password, m.email, m.allow_post, m.view_sigs, m.view_avs, m.view_pop, m.view_img, ".
  1517.                               "m.language, m.skin, m.new_msg, m.show_popup, m.msg_total, m.time_offset, m.posts, m.joined, m.last_post, ".
  1518.                               "m.last_visit, m.last_activity, m.dst_in_use, g.* FROM ibf_members m, ibf_groups g WHERE m.id='".$member_id."' and g.g_id=m.mgroup");
  1519.             
  1520.             if ( $DB->get_num_rows() )
  1521.             {
  1522.                 $this->member = $DB->fetch_row();
  1523.             }
  1524.             
  1525.             //-------------------------------------------------
  1526.             // Unless they have a member id, log 'em in as a guest
  1527.             //-------------------------------------------------
  1528.             
  1529.             if ( ($this->member['id'] == 0) or (empty($this->member['id'])) )
  1530.             {
  1531.                 $this->unload_member();
  1532.             }
  1533.         }
  1534.         
  1535.         unset($member_id);
  1536.     }
  1537.     
  1538.     //+-------------------------------------------------
  1539.     // Remove the users cookies
  1540.     //+-------------------------------------------------
  1541.     
  1542.     function unload_member()
  1543.     {
  1544.         global $DB, $std, $ibforums;
  1545.         
  1546.         // Boink the cookies
  1547.         
  1548.         $std->my_setcookie( "member_id" , "0", -1  );
  1549.         $std->my_setcookie( "pass_hash" , "0", -1  );
  1550.         
  1551.         $this->member['id']       = 0;
  1552.         $this->member['name']     = "";
  1553.         $this->member['password'] = "";
  1554.         
  1555.     }
  1556.     
  1557.     //-------------------------------------------
  1558.     // Updates a current session.
  1559.     //-------------------------------------------
  1560.     
  1561.     function update_member_session() {
  1562.         global $DB, $ibforums;
  1563.         
  1564.         // Make sure we have a session id.
  1565.         
  1566.         if ( (empty($this->session_id)) or ($this->session_id == 0) )
  1567.         {
  1568.             $this->create_member_session();
  1569.             return;
  1570.         }
  1571.         
  1572.         if (empty($this->member['id']))
  1573.         {
  1574.             $this->unload_member();
  1575.             $this->create_guest_session();
  1576.             return;
  1577.         }
  1578.         
  1579.         $query = "UPDATE ibf_sessions SET " .
  1580.                  "member_name='" .$this->member['name']     ."', ".
  1581.                  "member_pass='', ".
  1582.                  "member_id='"   .$this->member['id']       ."', ".
  1583.                  "member_group='".$this->member['mgroup']   ."', ";
  1584.         
  1585.         // Append the rest of the query
  1586.         $query .= "login_type='".$ibforums->input['Privacy']."', running_time='".$this->time_now."', location='".$ibforums->input['act'].",".$ibforums->input['f'].",".$ibforums->input['t'].",".$ibforums->input['p'].",".$ibforums->input['CODE']."' ";
  1587.         $query .= "WHERE id='".$this->session_id."'";
  1588.         
  1589.         // Update the database
  1590.         
  1591.         $DB->query($query);
  1592.     }        
  1593.     
  1594.     //--------------------------------------------------------------------
  1595.     
  1596.     function update_guest_session() {
  1597.         global $DB, $ibforums, $INFO;
  1598.         
  1599.         // Make sure we have a session id.
  1600.         
  1601.         if ( (empty($this->session_id)) or ($this->session_id == 0) )
  1602.         {
  1603.             $this->create_guest_session();
  1604.             return;
  1605.         }
  1606.         
  1607.         $query  = "UPDATE ibf_sessions SET member_name='',member_pass='',member_id='0',member_group='".$INFO['guest_group']."'";
  1608.         $query .= ",login_type='0', running_time='".$this->time_now."', location='".$ibforums->input['act'].",".$ibforums->input['f'].",".$ibforums->input['t'].",".$ibforums->input['p'].",".$ibforums->input['CODE']."' ";
  1609.         $query .= "WHERE id='".$this->session_id."'";
  1610.         
  1611.         // Update the database
  1612.         
  1613.         $DB->query($query);
  1614.     } 
  1615.                     
  1616.     
  1617.     //-------------------------------------------
  1618.     // Get a session based on the current session ID
  1619.     //-------------------------------------------
  1620.     
  1621.     function get_session($session_id="") {
  1622.         global $DB, $INFO, $std;
  1623.         
  1624.         $result = array();
  1625.         
  1626.         $query = "";
  1627.         
  1628.         $session_id = preg_replace("/([^a-zA-Z0-9])/", "", $session_id);
  1629.         
  1630.         if ( !empty($session_id) )
  1631.         {
  1632.         
  1633.             if ($INFO['match_browser'] == 1)
  1634.             {
  1635.                 $query = " AND browser='".$this->user_agent."'";
  1636.             }
  1637.                 
  1638.             $DB->query("SELECT id, member_id, running_time, location FROM ibf_sessions WHERE id='".$session_id."' and ip_address='".$this->ip_address."'".$query);
  1639.             
  1640.             if ($DB->get_num_rows() != 1)
  1641.             {
  1642.                 // Either there is no session, or we have more than one session..
  1643.                 
  1644.                 $this->session_dead_id   = $session_id;
  1645.                 $this->session_id        = 0;
  1646.                 $this->session_user_id   = 0;
  1647.                 return;
  1648.             }
  1649.             else
  1650.             {
  1651.                 $result = $DB->fetch_row();
  1652.                 
  1653.                 if ($result['id'] == "")
  1654.                 {
  1655.                     $this->session_dead_id   = $session_id;
  1656.                     $this->session_id        = 0;
  1657.                     $this->session_user_id   = 0;
  1658.                     unset($result);
  1659.                     return;
  1660.                 }
  1661.                 else
  1662.                 {
  1663.                     $this->session_id        = $result['id'];
  1664.                     $this->session_user_id   = $result['member_id'];
  1665.                     $this->last_click        = $result['running_time'];
  1666.                     $this->location          = $result['location'];
  1667.                     unset($result);
  1668.                     return;
  1669.                 }
  1670.             }
  1671.         }
  1672.     }
  1673.     
  1674.     //-------------------------------------------
  1675.     // Creates a member session.
  1676.     //-------------------------------------------
  1677.     
  1678.     function create_member_session() {
  1679.         global $DB, $INFO, $std, $ibforums;
  1680.         
  1681.         if ($this->member['id'])
  1682.         {
  1683.             //---------------------------------
  1684.             // Remove the defunct sessions
  1685.             //---------------------------------
  1686.             
  1687.             $INFO['session_expiration'] = $INFO['session_expiration'] ? (time() - $INFO['session_expiration']) : (time() - 3600);
  1688.             
  1689.             $DB->query( "DELETE FROM ibf_sessions WHERE running_time < {$INFO['session_expiration']} or member_id='".$this->member['id']."'");
  1690.             
  1691.             $this->session_id  = md5( uniqid(microtime()) );
  1692.             
  1693.             //---------------------------------
  1694.             // Insert the new session
  1695.             //---------------------------------
  1696.             
  1697.             $DB->query("INSERT INTO ibf_sessions (id, member_name, member_pass, member_id, ip_address, browser, running_time, location, login_type, member_group) ".
  1698.                        "VALUES ('".$this->session_id."', '".$this->member['name']."', '', '".$this->member['id']."', '".$this->ip_address."', '".$this->user_agent."', '".$this->time_now."', ".
  1699.                        "',,', '".$ibforums->input['Privacy']."', ".$this->member['mgroup'].")");
  1700.                        
  1701.             // If this is a member, update their last visit times, etc.
  1702.             
  1703.             if (time() - $this->member['last_activity'] > 300)
  1704.             {
  1705.                 //---------------------------------
  1706.                 // Reset the topics read cookie..
  1707.                 //---------------------------------
  1708.                 
  1709.                 $std->my_setcookie('topicsread', '');
  1710.                 
  1711.                 $DB->query("UPDATE ibf_members SET last_visit=last_activity, last_activity='".$this->time_now."' WHERE id='".$this->member['id']."'");
  1712.                 
  1713.                 //---------------------------------
  1714.                 // Fix up the last visit/activity times.
  1715.                 //---------------------------------
  1716.                 
  1717.                 $ibforums->input['last_visit']    = $this->member['last_activity'];
  1718.                 $ibforums->input['last_activity'] = $this->time_now;
  1719.             }
  1720.         }
  1721.         else
  1722.         {
  1723.             $this->create_guest_session();
  1724.         }
  1725.     }
  1726.     
  1727.     //--------------------------------------------------------------------
  1728.     
  1729.     function create_guest_session() {
  1730.         global $DB, $INFO, $std, $ibforums;
  1731.         
  1732.         //---------------------------------
  1733.         // Remove the defunct sessions
  1734.         //---------------------------------
  1735.         
  1736.         if ( ($this->session_dead_id != 0) and ( ! empty($this->session_dead_id) ) )
  1737.         {
  1738.             $extra = " or id='".$this->session_dead_id."'";
  1739.         }
  1740.         else
  1741.         {
  1742.             $extra = "";
  1743.         }
  1744.         
  1745.         $INFO['session_expiration'] = $INFO['session_expiration'] ? (time() - $INFO['session_expiration']) : (time() - 3600);
  1746.         
  1747.         $DB->query( "DELETE FROM ibf_sessions WHERE running_time < {$INFO['session_expiration']} or ip_address='".$this->ip_address."'".$extra);
  1748.         
  1749.         $this->session_id  = md5( uniqid(microtime()) );
  1750.         
  1751.         //---------------------------------
  1752.         // Insert the new session
  1753.         //---------------------------------
  1754.         
  1755.         $DB->query("INSERT INTO ibf_sessions (id, member_name, member_pass, member_id, ip_address, browser, running_time, location, login_type, member_group) ".
  1756.                    "VALUES ('".$this->session_id."', '', '', '0', '".$this->ip_address."', '".$this->user_agent."', '".$this->time_now."', ".
  1757.                    "',,', '0', ".$INFO['guest_group'].")");
  1758.                        
  1759.     }
  1760.     
  1761.     //--------------------------------------------------------------------
  1762.     
  1763.         
  1764. }
  1765.  
  1766.  
  1767.  
  1768.  
  1769. ?>